
$(function(){
    host="http://127.0.0.1:8080/admin/";
    tb3_1={
        editRow:undefined,
        add:function(){
            $('#save3_1,#redo3_1').show();
            if(this.editRow==undefined){
                $('#dg3_1').datagrid('insertRow',{
                    index:0,
                    row:{

                    }
                });
                $('#dg3_1').datagrid('beginEdit',0);
                this.editRow=0;
                //注意，隐藏列一定要写在去除editor之前！
                $("#dg3_1").datagrid("hideColumn", "id");
                $('#dg3_1').datagrid('removeEditor', 'id'); 
                 
             }

        },
        remove:function(){
            var rows=$('#dg3_1').datagrid('getSelections');
            console.log(rows[0]);
            if(rows.length>0){
                $.messager.confirm('确认信息','确定要删除记录?',function(r){
                    if (r){
                       $.ajax({
                           url: host+'department/remove',
                           type: 'POST',
                           dataType: 'json',
                           data: {id:rows[0].id},
                       })
                       .done(function(data) {
                            if(data.status==200){
                                 $('#dg3_1').datagrid('loaded');
                                 $('#dg3_1').datagrid('load');
                                 $('#dg3_1').datagrid('unselectAll');
                                 $.messager.show({
                                    title:'提示',
                                    msg:"您已成功删除",
                                 })
                             }else if(data.status==402){
                                $.messager.alert("警告","该部门下有雇员，删除失败",'warning');
                             }else if(data.status==401){
                                $.messager.alert("警告","没有该部门，删除失败",'warning');
                             }
                          
                       })
                       .fail(function() {
                           $.messager.alert("警告","ajax请求失败",'warning');
                       })
                       .always(function() {
                           
                       });
                       
                     }
            });
            }else{
                 $.messager.alert("提示","请至少选择一行要删除的记录",'info');
            }
             $('#dg3_1').datagrid('loaded');
             $('#dg3_1').datagrid('load');
             $('#dg3_1').datagrid('unselectAll');
        },
        edit:function(){
            $("#dg3_1").datagrid('addEditor', {    
                        field : 'id',    
                        editor : {    
                            type : 'validatebox',    
                            options : {
                                    required:true,
                                    }
                                }    
                    });  
            var rows=$('#dg3_1').datagrid('getSelections');

            if(rows.length==1){
                 if(tb3_1.editRow!=undefined){
                    $('#dg3_1').datagrid('endEdit',tb3_1.editRow);
                }
                if(tb3_1.editRow==undefined){
                   var rowIndex=$('#dg3_1').datagrid('getRowIndex',rows[0]);
                   $('#save3_1,#redo3_1').show();
                    $('#dg3_1').datagrid('beginEdit',rowIndex);
                    tb3_1.editRow=rowIndex;
                    //上一个点击了编辑的行，即使执行了endedit,但默仍为选中状态，
                    $('#dg3_1').datagrid('unselectRow',rowIndex);
                }
            }else{
                $.messager.alert("警告","修改只能选择一行",'warning');
            }
        },
        save:function(){
            $('#dg3_1').datagrid('endEdit',this.editRow);
          
        },
        redo:function(){
            $('#save3_1,#redo3_1').hide();
            this.editRow=undefined;
             $('#dg3_1').datagrid('rejectChanges');
                 $('#dg3_1').datagrid('showColumn','id');
        }
    };
   
//});
    $('#dg3_1').datagrid({
        url:host+'department/list', 
        width:800,
        queryParams:{
            start:0,
            length:10
        },
        title:'部门信息列表',
        fitColumns:true,
        columns:[[
            {   field:'id',
                title:'部门编号',
                width:100,
               
            },
           {
                field:'name',//field字段必须，为空也得写上，否则宽度为5px
                title:'部门名称',
                width:100,
                editor:{
                     type:'validatebox',
                    options:{

                        required:true,
                    }
                }
           }
        ]],
       
        pagination:true,
        pageSize:10,
        pageList:[10,20],
        pageNumber:1,
        toolbar:'#tb3_1',
        onSelectPage:function(pageNumber,pageSize){
            console.log(pageNumber+":"+pageSize);
        },
        //在endEdit后自动触发检验是否符合validate才会执行
        onAfterEdit:function(rowIndex,rowData,changes){
                $('#save3_1,#redo3_1').hide();
                var inserted=$('#dg3_1').datagrid('getChanges','inserted');
                var updated=$('#dg3_1').datagrid('getChanges','updated');
                //新增用户
                if(inserted.length>0){
                         $.ajax({
                           url: host+'department/create',
                           type: 'POST',
                           dataType: 'json',
                           data: {
                                    name:inserted[0].name,
                                },
                       })
                       .done(function(data) {
                            console.log(data);
                            if(data.status==200){
                                 $('#dg3_1').datagrid('loaded');
                                 $('#dg3_1').datagrid('load');
                                
                                 $.messager.show({
                                    title:'提示',
                                    msg:"您已成功添加",
                                 });
                                  tb3_1.editRow=undefined;
                             }
                       })
                       .fail(function() {
                        $('#dg3_1').datagrid('reload');
                            $.messager.alert("警告，添加失败！","不明错误",'warning');
                       })
                       .always(function() {
                          $("#dg3_1").datagrid("showColumn", "id");
                       });
                      
                
                }
                //编辑部门信息
                 if(updated.length>0){
                         $.ajax({
                           url: host+'department/set',
                           type: 'POST',
                           dataType: 'json',
                           data: {
                                    id:updated[0].id,
                                    name:updated[0].name,
                                },
                       })
                       .done(function(data) {
                            if(data.status==200){
                                 console.log("success");
                                 $('#dg3_1').datagrid('loaded');
                                 $('#dg3_1').datagrid('load');
                                
                                 $.messager.show({
                                    title:'提示',
                                    msg:"您已成功修改",
                                 });
                                  tb3_1.editRow=undefined;
                             }else if(data.status==401){
                                 $.messager.alert("警告，修改失败！","该部门id不存在",'warning');
                             }
                       })
                       .fail(function() {
                        $('#dg3_1').datagrid('reload');
                            $.messager.alert("警告，修改失败！","不明错误",'warning');
                       })
                       .always(function() {
                          
                       });
                
                }
                 $('#dg3_1').datagrid('unselectAll');
                 $('#dg3_1').datagrid('showColumn','id');
                 tb3_1.editRow=undefined;
               
        },//end  onAfterEdit:
        onDblClickRow:function(rowIndex,rowData){
            console.log("执行了双击操作");
            //console.log(rowData);
            if(tb1_1.editRow!=undefined){
                console.log("此时已有其他行处于编辑状态");
                $('#dg1_1').datagrid('endEdit',tb1_1.editRow);

            }
            if(tb1_1.editRow==undefined){
               console.log("此时没有其他行处于编辑状态");
               $('#save1_2,#redo1_2').show();
                $('#dg1_1').datagrid('beginEdit',rowIndex);
                tb1_1.editRow=rowIndex;
            }
        }//end ondblclickrow
    });

});
